package poker.controller;

import java.util.Calendar;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import poker.guimodel.UIState;
import at.ac.tuwien.big.easyholdem.player.DerbyPlayerDAO;
import at.ac.tuwien.big.easyholdem.player.Player;

public class RegisterController 
{
	private Player formPlayer;
	private DerbyPlayerDAO derbyPlayerDAO;

	protected UIState currentView;

	public String register()
	{
		derbyPlayerDAO.persist(formPlayer);
		
		currentView.setCurrentView(UIState.State.REGISTERED);
		
		return "registrationSuccessful";
	}
	
	public void validateBirthDate(FacesContext context, UIComponent toValidate, Object value) {
        Date gebDat = (Date) value;
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(gebDat);
        Calendar akt = Calendar.getInstance();
        akt.setTime(now);

        if (cal.get(Calendar.YEAR) < 1900 || gebDat.after(now)) {
            throw new RuntimeException();
        }
    }
	
	public void validateUsername(FacesContext context, UIComponent toValidate, Object value) {
		String username = (String) value;
		
		if(derbyPlayerDAO.getPlayerByUsername(username) != null) {
			throw new RuntimeException();
		}
	}
	
	public void validateStack(FacesContext context, UIComponent toValidate, Object value) {
		Integer stack = (Integer) value;

		if(stack < 1000) {
			throw new RuntimeException();
		}
	}
	
	public Player getFormPlayer() {
		return formPlayer;
	}

	public void setFormPlayer(Player formPlayer) {
		this.formPlayer = formPlayer;
	}
	
	public UIState getCurrentView() {
		return currentView;
	}

	public void setCurrentView(UIState currentView) {
		this.currentView = currentView;
	}

	public DerbyPlayerDAO getDerbyPlayerDAO() {
		return derbyPlayerDAO;
	}

	public void setDerbyPlayerDAO(DerbyPlayerDAO derbyPlayerDAO) {
		this.derbyPlayerDAO = derbyPlayerDAO;
	}
	
	
}
